package com.xmall.auth.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.auth.entity.SysMenu;
import com.xmall.auth.entity.SysMenuNode;
import com.xmall.auth.service.ISysMenuService;
import com.xmall.common.config.CommonPage;
import com.xmall.common.config.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author liuzh
 * @since 2020-06-11
 */

@Api(tags = "SysMenuController", description = "后台菜单管理" )
@RestController
@RequestMapping("/menu" )
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenMenuvice;

    @ApiOperation("创建后台菜单" )
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult registerMenu(@RequestBody SysMenu sysMenu) {
        updateLevel(sysMenu);
        boolean save = sysMenMenuvice.saveMenu(sysMenu);
        if (!save) {
            return CommonResult.failed();
        }
        return CommonResult.success(save);
    }

    /**
     * 修改菜单层级
     */
    private void updateLevel(SysMenu SysMenu) {
        if (SysMenu.getParentId() == 0) {
            //没有父菜单时为一级菜单
            SysMenu.setLevel(0);
        } else {
            //有父菜单时选择根据父菜单level设置
            SysMenu parentMenu = sysMenMenuvice.getItem(SysMenu.getParentId());
            if (parentMenu != null) {
                SysMenu.setLevel(parentMenu.getLevel() + 1);
            } else {
                SysMenu.setLevel(0);
            }
        }
    }

    @ApiOperation("删除后台用户" )
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult delete(@PathVariable Long id) {
        boolean delete = sysMenMenuvice.deleteMenu(id);
        if (!delete) {
            return CommonResult.failed();
        }
        return CommonResult.success(delete);
    }

    @ApiOperation("更新后台用户" )
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody SysMenu sysMenu) {
        boolean update = sysMenMenuvice.updateMenu(id, sysMenu);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success(update);
    }

    @ApiOperation("获取后台菜单ById" )
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SysMenu> getItem(@PathVariable Long id) {
        SysMenu SysMenu = sysMenMenuvice.getItem(id);
        return CommonResult.success(SysMenu);
    }

    @ApiOperation("分页查询后台菜单" )
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SysMenu>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                  @RequestParam(value = "pageSize", defaultValue = "5" ) Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1" ) Integer pageNum) {

        Page<SysMenu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(keyword)) {
            queryWrapper.like("menu_name", keyword);
        }
        IPage<SysMenu> MenuIPage = sysMenMenuvice.page(page, queryWrapper);
        return CommonResult.success(CommonPage.restPage(MenuIPage));
    }

    @ApiOperation("分页查询后台菜单" )
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SysMenu>> list(@PathVariable Long parentId) {
        List<SysMenu> menuList = sysMenMenuvice.listByParentId(parentId);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("树形结构返回所有菜单列表" )
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<SysMenuNode>> treeList() {
        List<SysMenuNode> list = sysMenMenuvice.treeList();
        return CommonResult.success(list);
    }

    @ApiOperation("修改菜单显示状态" )
    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateHidden(@PathVariable Long id, @RequestParam("hidden" ) Integer hidden) {

        boolean update = sysMenMenuvice.updateHidden(id, hidden);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success(update);
    }


}
